package eco2automotive.util;

import java.net.URL;
import java.io.File;
/**  
  * Obontains the user directory
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  *
  */ 
public class WorkingDirectory {

    private static File WORKING_DIRECTORY;

    /**
     * 
     * @return WORKING_DIRECTORY 
     */
    public static File get() {
        if(WORKING_DIRECTORY == null) {
            try {
                URL url = WorkingDirectory.class.getResource("example.txt");
                System.out.println(url);
                if(url.getProtocol().equals("file")) {
                    File f = new File(url.toURI());
                    f = f.getParentFile()
                    .getParentFile()
                    .getParentFile();
                    WORKING_DIRECTORY = f;
                } else if(url.getProtocol().equals("jar")) {
                    String expected = "!/util/example.txt";
                    String s = url.toString();
                    s = s.substring(4);
                    s = s.substring(0, s.length() - expected.length());
                    File f = new File(new URL(s).toURI());
                    f = f.getParentFile();
                    WORKING_DIRECTORY = f;
                }
            } catch(Exception e) {
                WORKING_DIRECTORY = new File(".");
            }
        }
        return WORKING_DIRECTORY;
    }
    /**
     * 
     * @return String whit the user directory
     */
    public String workingDirectoryJar()
    {
          //System.out.print(System.getProperty("user.dir"));
        return System.getProperty("user.dir"); 
    }
 
}